package com.t4.courseware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.t4.courseware.common.Result;
import com.t4.courseware.handler.UserThreadLocal;
import com.t4.courseware.mapper.CoursewareMapper;
import com.t4.courseware.mapper.UserCoursewareMapper;
import com.t4.courseware.pojo.Courseware;
import com.t4.courseware.pojo.UserCourseware;
import com.t4.courseware.pojo.dto.UserCoursewareDto;
import com.t4.courseware.pojo.dto.UserDto;
import com.t4.courseware.service.UserCoursewareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class UserCoursewareServiceImpl extends ServiceImpl<UserCoursewareMapper, UserCourseware> implements UserCoursewareService {
    @Resource
    private UserCoursewareMapper userCoursewareMapper;
    @Resource
    private CoursewareMapper coursewareMapper;

    // 在上传/兑换时，就已经更新了 user_courseware，所以这里不需要再做去重
    @Override
    public Result getMyCourseware() {
        UserDto userDto = UserThreadLocal.get();
        Long id = userDto.getId();
        List<UserCoursewareDto> myCourseware = userCoursewareMapper.findMyCourseware(id);
//        HashSet<UserCoursewareDto> set = new HashSet<>(myCourseware);
//        coursewareMapper.selectList(new LambdaQueryWrapper<Courseware>().eq(Courseware::getUploadUserId, id)).forEach(i -> {
//            UserCoursewareDto userCoursewareDto = new UserCoursewareDto();
//            userCoursewareDto.setUserId(id);
//            userCoursewareDto.setCourseware(i);
//            userCoursewareDto.setCwId(i.getId());
//            userCoursewareDto.setCreateTime(i.getCreateTime());
//            set.add(userCoursewareDto);
//        });
//        List<UserCoursewareDto> res = new ArrayList<>(set);
//        myCourseware = myCourseware.stream().peek(userCoursewareDto -> userCoursewareDto.getCourseware().setUrl(aliyunConfig.getUrl() + userCoursewareDto.getCourseware().getUrl())).collect(Collectors.toList());
        return Result.SUCCESS(myCourseware);
    }
}
